/***************************
NO.01 根据输入的mtrl_cate,query bis_bom,bis_mtrl .返回mtrl_id,mtrl_mkr

/***************************/
package com.service;

import static com.def.GenericDef.E_XPAPLYDN_DN_WO_NOT_FOUND;
import static com.def.GenericDef.E_XPAPLYDN_OTHER_ERROR;
import static com.def.GenericDef.E_XPAPLYWO_OTHER_ERROR;
import static com.def.GenericDef.PROC_COMPLETE;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IRetShipNoticeDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Ret_ship_notice;
import com.model.Ret_wo;
import com.tx.Xpaplydn.XpaplydnI;
import com.tx.Xpaplydn.XpaplydnIA;
import com.tx.Xpaplydn.XpaplydnO;
import com.tx.Xpaplydn.XpaplydnOA;
import com.tx.Xpaplydn.XpaplydnOB;
import com.util.GUIDGenerator;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpaplydnService")
public class XpaplydnService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpaplydnService.class);
	@Autowired
	XpaplydnI inTrx;

	@Autowired
	XpaplydnO outTrx;

	@Autowired
	private IRetWODao retWODao;

	@Autowired
	private IRetShipNoticeDao retShipNoticeDao;

	@Autowired
	private MqOpe mqOpe;
	
	/******宏定义******/
	public final static String WO_TYPE_WORDER = "W";
	static String hql;
	public String subMainProc(String strInMsg) {
		String reply = "";
		try {
			inTrx = (XpaplydnI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);
			outTrx.setTrx_id("XPAPLYDN");
			outTrx.setType_id("O");

			String actionFlg = inTrx.getAction_flg();
			if (actionFlg.equals("Q")) {
				getDnProc();
			} else if (actionFlg.equals("D")) {
				delDnProc();
			} else if (actionFlg.equals("U")) {
				updDnProc();
			} else if (actionFlg.equals("A")) {
				addDnProc();
			} else if (actionFlg.equals("F")) {
				getDistinctDnNoProc();
			} else if (actionFlg.equals("M")) {
				summaryDnWoInfProc();
			} else if (actionFlg.equals("X")) {//关闭DN
				closeDNProc();
			} else if (actionFlg.equals("Y")) {//打开DN
				openDNProc();
			} else if (actionFlg.equals("G")){
				getDnProcquchong();
			} else {
				errActionFnc();
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retShipNoticeDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			logger.error(Util.stackTraceToString(ex));
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());

		} finally {
			reply = mqOpe.obj2Str(outTrx);
			logger.info(reply);
		}
		return reply;
	}

	public void getDnProc() throws ParseException {
		List<XpaplydnOA> oaryList = new ArrayList<XpaplydnOA>();
			XpaplydnIA iary = inTrx.getIary().get(0);
			String dn_no = iary.getDn_no();
			String dn_stat = iary.getDn_stat();
			String so_id_fk = iary.getSo_id_fk();
			String wo_id_fk = iary.getWo_id_fk();
			String cus_id = iary.getCus_id();
			String evt_seq_id = iary.getEvt_seq_id();
			String datein = iary.getDate();
			String timein = iary.getTime();
			Date date = null,time = null;
			if(datein != null){
				 date = java.sql.Date.valueOf(datein);
	    	}
			if(timein != null){
				 time = java.sql.Time.valueOf(timein);
			}
			String sql = "FROM Ret_ship_notice where 1=1  ";
			if (dn_no != null) {
				sql += " AND dn_no='" + dn_no + "'";
			}
			if (so_id_fk != null) {
				sql += " AND so_id_fk like " + so_id_fk + "'%'";
			}
			if (wo_id_fk != null) {
				sql += " AND wo_id_fk='" + wo_id_fk + "'";
			}
			if (dn_stat != null) {
				sql += " AND dn_stat='" + dn_stat + "'";
			}
			if (cus_id != null) {
				sql += " AND cus_id_fk='" + cus_id + "'";
			}
			if (evt_seq_id != null) {
				sql += " AND evt_seq_id='" + evt_seq_id + "'";
			}
			if(date !=null){
				sql += " AND ship_date='" + date + "'";
			}
			if(time !=null){
				sql += " AND ship_time='" + time + "'";
			}
			List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(sql);

			if (retShipNoticeList != null) {
				for (Ret_ship_notice retShipNotice : retShipNoticeList) {
					XpaplydnOA oary = new XpaplydnOA();
					oary.setEvt_seq_id(retShipNotice.getEvt_seq_id());
					oary.setDn_no(retShipNotice.getDn_no());
					oary.setDn_stat(getStatDsc(retShipNotice.getDn_stat()));//DN状态装换中文
					oary.setShip_address(retShipNotice.getShip_address());
					oary.setShip_car_id(retShipNotice.getShip_car_id());
					if (retShipNotice.getShip_date() != null) {
						oary.setShip_date(retShipNotice.getShip_date());
					}
					oary.setShip_note(retShipNotice.getShip_note());
					oary.setShip_qty(retShipNotice.getShip_qty());
					oary.setShip_time(retShipNotice.getShip_time());
					oary.setShip_usr(retShipNotice.getShip_usr());
					oary.setEvt_usr(retShipNotice.getEvt_usr());
					oary.setEvt_timestamp(retShipNotice.getEvt_timestamp());
					oary.setDn_wh_out_prd_qty(retShipNotice.getWh_out_prd_qty());
					oary.setSo_id_fk(retShipNotice.getSo_id_fk());
					oary.setWo_id_fk(retShipNotice.getWo_id_fk());

					oary.setCus_id(retShipNotice.getCus_id_fk());
					oary.setShip_cus_id(retShipNotice.getShip_cus_id_fk());
					oary.setCrt_timestamp(retShipNotice.getCrt_timestamp());
					oary.setCrt_usr(retShipNotice.getCrt_usr());
					oary.setShipping_timestamp(retShipNotice.getShipping_timestamp());
					if(retShipNotice.getShip_date() != null&&retShipNotice.getShip_time()!=null){
					   oary.setPlan_shipping_timestamp(retShipNotice.getShip_date().toString()+"_"+retShipNotice.getShip_time().toString());
					}
					oary.setDn_unique(retShipNotice.getDn_no()+"/"+oary.getPlan_shipping_timestamp()+"@"+retShipNotice.getEvt_seq_id());
					oary.setDn_uniquetime(retShipNotice.getDn_no()+"/"+oary.getPlan_shipping_timestamp());
					DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Timestamp ts = new Timestamp(format.parse(retShipNotice.getShip_date().toString()+" "+retShipNotice.getShip_time().toString()).getTime());
					oary.setPlan_timestamp(ts);
					Ret_wo retWo = retWODao.get(retShipNotice.getWo_id_fk());
					if(retWo!=null){
						oary.setMtrl_prod_id_fk(retWo.getMtrl_prod_id_fk());
						oary.setMdl_id_fk(retWo.getMdl_id_fk());
						oary.setFm_mdl_id_fk(retWo.getFm_mdl_id_fk());
						oary.setCut_mdl_id_fk(retWo.getCut_mdl_id_fk());
						oary.setPln_prd_qty(retWo.getPln_prd_qty());
						oary.setWh_in_prd_qty(retWo.getWh_in_prd_qty()+retWo.getWip_wh_in_prd_qty());
						oary.setWh_out_prd_qty(retWo.getWh_out_prd_qty());
						oary.setScrp_prd_qty(retWo.getScrp_prd_qty());
						oary.setFrom_thickness(retWo.getFrom_thickness());
						oary.setTo_thickness(retWo.getTo_thickness());
						oary.setRl_prd_qty(retWo.getRl_prd_qty());
						oary.setWo_rsv_dn_qty(retWo.getRsv_dn_qty());
						oary.setWo_space_dn_qty(retWo.getRl_prd_qty()-retWo.getRsv_dn_qty());
						
						String so_id_str = retWo.getSo_id();
						if (!Util.isSpaceCheck(so_id_str) && so_id_str.length() > 2) {
							if (so_id_str.substring(0, 2).equals("DQR")) {
							}else {
								oary.setReal_so_id(so_id_str);
							}
						}else {
							oary.setReal_so_id("");
						}
					}else {
						String so_id_str = retShipNotice.getSo_id_fk();
						if (!Util.isSpaceCheck(so_id_str) && so_id_str.length() > 2) {
							if (so_id_str.substring(0, 3).equals("DQR")) {
							}else {
								oary.setReal_so_id(so_id_str);
							}
						}else {
							oary.setReal_so_id("");
						}
					}
					oaryList.add(oary);
				}
			}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}

	public void addDnProc() throws ParseException {
		outTrx.setTrx_id("XPAPLYWO");
		outTrx.setType_id("O");
		Timestamp crTimestamp = Util.getCurrentTimestamp();
		long tbl_cnt = inTrx.getTbl_cnt();
		for (int i = 0; i < tbl_cnt; i++) {

			XpaplydnIA iary = inTrx.getIary().get(i);
			String woId = iary.getWo_id_fk();
			String hql = "FROM Ret_ship_notice where dn_no = ?";
			List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.list(hql, iary.getDn_no()); 
			for(Ret_ship_notice retShipNotice : retShipNoticeList){
				if(!retShipNotice.getCus_id_fk().equals(iary.getCus_id())){
					outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
					outTrx.setRtn_mesg("此单与系统中已存在的交货单的客户ID不相同");
					return;
				}
			}
			
			Ret_wo retWo = retWODao.getWithLock(iary.getWo_id_fk(),LockMode.UPGRADE);
			if (retWo == null) {
				outTrx.setRtn_code(E_XPAPLYDN_DN_WO_NOT_FOUND + "");
				outTrx.setRtn_mesg("内部订单[" + iary.getWo_id_fk() + "]没有建立，错误!!!");
				return;
			}
			if(retWo.getSo_id()==null){
				outTrx.setRtn_code(E_XPAPLYDN_DN_WO_NOT_FOUND + "");
				outTrx.setRtn_mesg("内部订单[" + iary.getWo_id_fk() + "]没有和商务订单绑定，错误!!!");
				return;
			}
			retWo.setRsv_dn_qty(retWo.getRsv_dn_qty()+iary.getShip_qty());
			retWODao.update(retWo);


			Ret_ship_notice newRetShipNotice = new Ret_ship_notice();
			newRetShipNotice.setEvt_seq_id(GUIDGenerator.javaGUID());
			newRetShipNotice.setDn_no(iary.getDn_no());
			newRetShipNotice.setDn_stat("WAIT");
			newRetShipNotice.setSo_id_fk(retWo.getSo_id());
			newRetShipNotice.setWo_id_fk(iary.getWo_id_fk());
			newRetShipNotice.setShip_date(iary.getShip_date());
			newRetShipNotice.setShip_time(iary.getShip_time());
			newRetShipNotice.setShip_qty(iary.getShip_qty());
			newRetShipNotice.setShip_address(iary.getShip_address());
			newRetShipNotice.setShip_usr(iary.getShip_usr());
			newRetShipNotice.setShip_car_id(iary.getShip_car_id());
			newRetShipNotice.setEvt_usr(iary.getEvt_usr());
			newRetShipNotice.setEvt_timestamp(crTimestamp);
			newRetShipNotice.setShip_note(iary.getShip_note());
			newRetShipNotice.setWh_out_prd_qty(0);
			newRetShipNotice.setEvt_cate("BULD");
			newRetShipNotice.setCrt_usr(iary.getEvt_usr());
			newRetShipNotice.setCrt_timestamp(crTimestamp);
			newRetShipNotice.setCus_id_fk(iary.getCus_id());
			newRetShipNotice.setShip_cus_id_fk(iary.getShip_cus_id());
			newRetShipNotice.setCus_mix_flg("N");//TODO

//			int currentQty = 0;
//			hql = "FROM Ret_ship_notice where wo_id_fk = ?";
//			retShipNoticeList = retShipNoticeDao.list(hql, woId);
//			for(Ret_ship_notice oldRetShipNotice : retShipNoticeList){
//				currentQty = currentQty + oldRetShipNotice.getShip_qty();
//			}
//			
//			int totalQty = retWo.getRl_prd_qty();
//			
//			if(currentQty + newRetShipNotice.getShip_qty() >totalQty){
//				outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
//				outTrx.setRtn_mesg("交货订单累计数量已经超过订单产品下线数量,本次最多交货数量为["+(totalQty-currentQty)+"]");
//				retur取消一个内部订单只能创建一个交货单逻辑n;
//			}

			//取消一			int dnCnt = checkDn(iary.getWo_id_fk(),iary.getDn_no(),iary.getShip_date().toString(),iary.getShip_time().toString());
//			if (dnCnt > 0 ) {
//				outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+1+"");
//				outTrx.setRtn_mesg("["+iary.getWo_id_fk()+"],["+
//						iary.getDn_no()+"],["+
//						iary.getShip_date().toString()+"],["+
//						iary.getShip_time().toString()+"]已经存在！！");
//				return;
//			}
//			int dnCntbywotime = checkWotime(iary.getWo_id_fk(),iary.getShip_date().toString(),iary.getShip_time().toString());
//			if (dnCntbywotime > 0 ) {
//				outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+1+"");
//				outTrx.setRtn_mesg("同一内部订单["+iary.getWo_id_fk()+"],同一计划出货时间["+
//						iary.getShip_date().toString()+" "+
//						iary.getShip_time().toString()+"]不能建立不同交货订单！");
//				return;
//			}
			
			retShipNoticeDao.save(newRetShipNotice);

		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		getDnProc();
	}

	public void delDnProc() {

		for (XpaplydnIA iary : inTrx.getIary()) {
			String hql = "FROM Ret_ship_notice where evt_seq_id = '"+iary.getEvt_seq_id()+"'";
			List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(hql);
			for (Ret_ship_notice ret_ship_notice : retShipNoticeList) {
				if(ret_ship_notice.getWh_out_prd_qty() > 0){
					
					if(ret_ship_notice.getShip_qty() == ret_ship_notice.getWh_out_prd_qty()){
						outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR + "");
						outTrx.setRtn_mesg("该交货订单已完成交货,不能删除");
						return;
					}else{
						outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR + "");
						outTrx.setRtn_mesg("该交货订单正在交货,不能删除");
						return;
					}
				}
				retShipNoticeDao.delete(ret_ship_notice);
				Ret_wo retWo = retWODao.getWithLock(ret_ship_notice.getWo_id_fk(), LockMode.UPGRADE);
				retWo.setRsv_dn_qty(retWo.getRsv_dn_qty() - ret_ship_notice.getShip_qty());
				retWODao.update(retWo);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}

	public void updDnProc() throws ParseException {
		for (XpaplydnIA iary : inTrx.getIary()) {
			String woId = iary.getWo_id_fk();
			String dnNo = iary.getDn_no();
			String evt_seq_id = iary.getEvt_seq_id();
			if (Util.isSpaceCheck(evt_seq_id)) {
				outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR + "");
				outTrx.setRtn_mesg("该DN["+dnNo+"]的evt_seq_id为空!!");
				return;
			}

			String hql = "FROM Ret_ship_notice where evt_seq_id = '"+evt_seq_id+"'";
			List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(hql);
			if (!retShipNoticeList.isEmpty()) {
				for (Ret_ship_notice newRetShipNotice : retShipNoticeList) {
					if (iary.getDn_stat() != null) {
						newRetShipNotice.setDn_stat(iary.getDn_stat());
					}
					if (iary.getShip_address() != null) {
						newRetShipNotice.setShip_address(iary.getShip_address());
					}
					if (iary.getShip_car_id() != null) {
						newRetShipNotice.setShip_car_id(iary.getShip_car_id());
					}
					if (iary.getShip_date() != null) {
						newRetShipNotice.setShip_date(iary.getShip_date());
					}
					if (iary.getShip_time() != null) {
						newRetShipNotice.setShip_time(iary.getShip_time());
					}
					if (iary.getShip_note() != null) {
						newRetShipNotice.setShip_note(iary.getShip_note());
					}
					if( newRetShipNotice.getDn_stat().equals(PROC_COMPLETE) &&  iary.getShip_qty()!= newRetShipNotice.getShip_qty()){
						outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR + "");
						outTrx.setRtn_mesg("已经完成出货的交货单 不能更改数量");
						return;
					}
					
					if (iary.getShip_usr() != null) {
						newRetShipNotice.setShip_usr(iary.getShip_usr());
					}
					if (iary.getEvt_usr() != null) {
						newRetShipNotice.setEvt_usr(iary.getEvt_usr());
					}
					if(iary.getCus_id()!=null){
						newRetShipNotice.setCus_id_fk(iary.getCus_id());
					}
					if(iary.getShip_cus_id()!=null){
						newRetShipNotice.setShip_cus_id_fk(iary.getShip_cus_id());
					}

					int oldDNQty = newRetShipNotice.getShip_qty();
					
					if (iary.getShip_qty() != null) {
						newRetShipNotice.setShip_qty(iary.getShip_qty());
					}

					if(iary.getWo_id_fk()!=null){
						Ret_wo retWo = retWODao.getWithLock(woId,LockMode.UPGRADE);
						if (retWo == null) {
							outTrx.setRtn_code(E_XPAPLYDN_DN_WO_NOT_FOUND + "");
							outTrx.setRtn_mesg("商务订单[" + iary.getWo_id_fk() + "]没有建立，错误!!!");
							return;
						}
						
						
						newRetShipNotice.setWo_id_fk(iary.getWo_id_fk());
						newRetShipNotice.setSo_id_fk(retWo.getSo_id());
						
						int currentQty = 0;
						hql = "FROM Ret_ship_notice where wo_id_fk = ? and dn_no <> ?";
						retShipNoticeList = retShipNoticeDao.list(hql, woId,dnNo);
						for(Ret_ship_notice oldRetShipNotice : retShipNoticeList){
							currentQty = currentQty + oldRetShipNotice.getShip_qty();
						}
						
						int totalQty = retWo.getRl_prd_qty();
						
						if(currentQty + newRetShipNotice.getShip_qty()  >totalQty){
							outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
							outTrx.setRtn_mesg("交货订单累计数量已经超过订单总数,本次最多交货数量为["+(totalQty-currentQty)+"]");
							return;
						}
						
						retWo.setRsv_dn_qty(retWo.getRsv_dn_qty() - oldDNQty +iary.getShip_qty());
						retWODao.update(retWo);
					}
					if(newRetShipNotice.getShip_qty()<newRetShipNotice.getWh_out_prd_qty()){
						outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+"");
						outTrx.setRtn_mesg("交货数量不能小于实际出货产品数量");
						return ;
					}
					newRetShipNotice.setEvt_timestamp(Util.getCurrentTimestamp());
					
					retShipNoticeDao.update(newRetShipNotice);
					int dnCnt = checkDn(iary.getWo_id_fk(),iary.getDn_no(),iary.getShip_date().toString(),iary.getShip_time().toString());
					if (dnCnt > 1 ) {
						outTrx.setRtn_code(E_XPAPLYDN_OTHER_ERROR+1+"");
						outTrx.setRtn_mesg("["+iary.getWo_id_fk()+"],["+
								iary.getDn_no()+"],["+
								iary.getShip_date().toString()+"],["+
								iary.getShip_time().toString()+"]已经存在！！");
						return;
					}
					
				}
			} else {
				outTrx.setRtn_code(E_XPAPLYDN_DN_WO_NOT_FOUND + "");
				outTrx.setRtn_mesg("出货通知单号[" 
						+ iary.getDn_no() + "] 和内部订单号 ["
						+ iary.getWo_id_fk() + "]未绑定！");
				return;
	
			}
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		getDnProc();
	}

	public void getDistinctDnNoProc() {
		String sql = "select distinct dn_no FROM Ret_ship_notice ";
		List<String> retShipNoticeList = retShipNoticeDao.findDistStringList(sql);
		List<XpaplydnOA> oaryList = new ArrayList<XpaplydnOA>();
		if (retShipNoticeList != null && retShipNoticeList.size() > 0) {
			for (String dn_no : retShipNoticeList) {
				XpaplydnOA oary = new XpaplydnOA();
				oary.setDn_no(dn_no);
				oaryList.add(oary);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}
	
	private void summaryDnWoInfProc() {
		String dn_no="";
		XpaplydnIA iary = inTrx.getIary().get(0);
		List<XpaplydnOB> oaryBlist = new ArrayList<XpaplydnOB>();
		dn_no = iary.getDn_no();
		//time
		Timestamp zdfrom_date = inTrx.getZdfrom_date();
	    Timestamp zdto_date = inTrx.getZdto_date();
		Date jhfrom_date = inTrx.getJhfrom_date();
		Date jhto_date = inTrx.getJhto_date();
		
		hql = "FROM Ret_ship_notice where 1=1";
		if(!dn_no.equals("")){
			hql += " AND dn_no ='" + dn_no + "'";
		}
		if(zdfrom_date!=null){
			hql += " AND crt_timestamp >= '" + zdfrom_date + "'";
		}
		if(zdto_date!=null){
			hql += " AND crt_timestamp <= '" + zdto_date + "'";
		}
		if(jhfrom_date!=null){
			hql += " AND ship_date >= '" + jhfrom_date + "'";
		}
		if(jhto_date!=null){
			hql += " AND ship_date <= '" + jhto_date + "'";
		}

		List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(hql);
		if(retShipNoticeList.size()!=0){
			for(Ret_ship_notice ret_ship_notice : retShipNoticeList){
				XpaplydnOB oaryB = new XpaplydnOB();
				Integer wip_qty=0;
				Integer no_out_prd_qty=0;
				String wo_id = ret_ship_notice.getWo_id_fk();
				Ret_wo ret_wo = retWODao.get(wo_id);
				if(ret_wo!=null){
					oaryB.setWo_id_fk(wo_id);
					oaryB.setMdl_id_fk(ret_wo.getMdl_id_fk());
					oaryB.setWh_in_prd_qty(ret_wo.getWh_in_prd_qty().toString());
					wip_qty = ret_wo.getWh_in_prd_qty()==null ? ret_wo.getRl_prd_qty() :
								(ret_wo.getRl_prd_qty()-ret_wo.getWh_in_prd_qty()-ret_wo.getComp_prd_qty()-ret_wo.getWh_out_prd_qty());
					oaryB.setWip_qty(wip_qty);
					oaryB.setPln_prd_qty(ret_wo.getPln_prd_qty().toString());
				}
				oaryB.setDn_no(ret_ship_notice.getDn_no());
				oaryB.setShip_qty(ret_ship_notice.getShip_qty().toString());
				no_out_prd_qty = ret_ship_notice.getWh_out_prd_qty()== null ? ret_ship_notice.getShip_qty(): 
					(ret_ship_notice.getShip_qty()-ret_ship_notice.getWh_out_prd_qty());
				oaryB.setWh_out_prd_qty(ret_ship_notice.getWh_out_prd_qty().toString());
				oaryB.setNo_out_prd_qty(no_out_prd_qty);
				oaryB.setCus_id_fk(ret_ship_notice.getCus_id_fk());
				oaryB.setDn_stat(ret_ship_notice.getDn_stat());
				oaryB.setShip_date(ret_ship_notice.getShip_date());
				oaryB.setCrt_usr(ret_ship_notice.getCrt_usr());
				oaryB.setCrt_timestamp(ret_ship_notice.getCrt_timestamp());
				
				if (ret_ship_notice.getShipping_timestamp() != null){
					oaryB.setShipping_timestamp(ret_ship_notice.getShipping_timestamp());
				}
				if(ret_ship_notice.getShip_date() != null&&ret_ship_notice.getShip_time()!=null){
					oaryB.setPlan_shipping_timestamp(ret_ship_notice.getShip_date().toString()+" "+ret_ship_notice.getShip_time().toString());
					}
				oaryBlist.add(oaryB);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOaryB(oaryBlist);
		outTrx.setTbl_cnt(oaryBlist.size());
	}
	public void errActionFnc() {
		outTrx.setTrx_id("XPAPLYDN");
		outTrx.setType_id("O");
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}

	public String comObj2String(Object obj) {
		if (obj == null) {
			return "";
		}
		return obj.toString();
	}
	public Integer comObj2Int(Object obj) {
		if (obj == null) {
			return 0;
		}
		return Integer.valueOf(obj.toString());
	}
	private boolean checkShipQty(Ret_wo retWo,Ret_ship_notice newRetShipNotice){
		int currentQty = 0;
		String woId = retWo.getWo_id();
		String hql = "FROM Ret_ship_notice where wo_id_fk = ?";
		List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.list(hql, woId);
		for(Ret_ship_notice oldRetShipNotice : retShipNoticeList){
			currentQty = currentQty + oldRetShipNotice.getShip_qty();
		}
		currentQty = currentQty + newRetShipNotice.getShip_qty();
		
		int totalQty = retWo.getPln_prd_qty();
		
		if(currentQty>totalQty){
			return false;
		}
		return true;
	}
	
	private String getStatDsc(String dnStat) {
		if (dnStat.equals("WAIT")) {
			return "等待";
		} else if (dnStat.equals("COMP")){
			return "完成";
		}
		return "其他";
	}
	
	private boolean checkShipQty(Ret_wo retWo){
		int currentQty = 0;
		String woId = retWo.getWo_id();
		String hql = "FROM Ret_ship_notice where wo_id_fk = ?";
		List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.list(hql, woId);
		for(Ret_ship_notice oldRetShipNotice : retShipNoticeList){
			currentQty = currentQty + oldRetShipNotice.getShip_qty();
		}
		
		int totalQty = retWo.getPln_prd_qty();
		
		if(currentQty>totalQty){
			return false;
		}
		return true;
	}
	
	private void closeDNProc() {
		XpaplydnIA iary = inTrx.getIary().get(0);
		String hql = "FROM Ret_ship_notice where" + " dn_no='" + iary.getDn_no() + "'"
		+ "and wo_id_fk='" + iary.getWo_id_fk() + "'";
		List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(hql);
		for (Ret_ship_notice ret_ship_notice : retShipNoticeList) {
			ret_ship_notice.setDn_stat("COMP");
			ret_ship_notice.setEvt_usr(iary.getEvt_usr());
			retShipNoticeDao.update(ret_ship_notice);
		}
	}

	private void openDNProc() {
		XpaplydnIA iary = inTrx.getIary().get(0);
		String hql = "FROM Ret_ship_notice where" + " dn_no='" + iary.getDn_no() + "'"
		+ "and wo_id_fk='" + iary.getWo_id_fk() + "'";
		List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(hql);
		for (Ret_ship_notice ret_ship_notice : retShipNoticeList) {
			ret_ship_notice.setDn_stat("WAIT");
			ret_ship_notice.setEvt_usr(iary.getEvt_usr());
			retShipNoticeDao.update(ret_ship_notice);
		}
	}
	
	@SuppressWarnings("unchecked")
	private int checkDn(String wo_id_fk,String dn_no,String ship_date,String ship_time){
		List<Ret_ship_notice> retShipNoticeList = new ArrayList<Ret_ship_notice>();
		StringBuffer sb = new StringBuffer("FROM Ret_ship_notice where wo_id_fk = '");
		sb.append(wo_id_fk)
//		  .append("' and dn_no = '")
//		  .append(dn_no)
//		  .append("' and ship_date = '")
//		  .append(ship_date)
//		  .append("' and ship_time = '")
//		  .append(ship_time)
		  .append("'");
		
		retShipNoticeList = retShipNoticeDao.find(sb.toString());
		if (retShipNoticeList.isEmpty()) {
			return 0;
		}else {
			return retShipNoticeList.size();
		}
	}
	
	@SuppressWarnings("unchecked")
	private int checkWotime(String wo_id_fk,String ship_date,String ship_time){
		List<Ret_ship_notice> retShipNoticeList = new ArrayList<Ret_ship_notice>();
		StringBuffer sb = new StringBuffer("FROM Ret_ship_notice where wo_id_fk = '");
		sb.append(wo_id_fk)
		  .append("' and ship_date = '")
		  .append(ship_date)
		  .append("' and ship_time = '")
		  .append(ship_time)
		  .append("'");
		
		retShipNoticeList = retShipNoticeDao.find(sb.toString());
		if (retShipNoticeList.isEmpty()) {
			return 0;
		}else {
			return retShipNoticeList.size();
		}
	}
//	public void getDnProcquchong() throws ParseException {
//		List<XpaplydnOA> oaryList = new ArrayList<XpaplydnOA>();
//		XpaplydnIA iary = inTrx.getIary().get(0);
//		String dn_no = iary.getDn_no();
//		String dn_stat = iary.getDn_stat();
//		String so_id_fk = iary.getSo_id_fk();
//		String wo_id_fk = iary.getWo_id_fk();
//		String cus_id = iary.getCus_id();
//		String evt_seq_id = iary.getEvt_seq_id();
//		String datein = iary.getDate();
//		String timein = iary.getTime();
//		Date date = null,time = null;
//		if(datein != null){
//			 date = java.sql.Date.valueOf(datein);
//    	}
//		if(timein != null){
//			 time = java.sql.Time.valueOf(timein);
//		}
//		String sql = "FROM Ret_ship_notice where 1=1  ";
//		if (dn_no != null) {
//			sql += " AND dn_no='" + dn_no + "'";
//		}
//		if (so_id_fk != null) {
//			sql += " AND so_id_fk like " + so_id_fk + "'%'";
//		}
//		if (wo_id_fk != null) {
//			sql += " AND wo_id_fk='" + wo_id_fk + "'";
//		}
//		if (dn_stat != null) {
//			sql += " AND dn_stat='" + dn_stat + "'";
//		}
//		if (cus_id != null) {
//			sql += " AND cus_id_fk='" + cus_id + "'";
//		}
//		if (evt_seq_id != null) {
//			sql += " AND evt_seq_id='" + evt_seq_id + "'";
//		}
//		if(date !=null){
//			sql += " AND ship_date='" + date + "'";
//		}
//		if(time !=null){
//			sql += " AND ship_time='" + time + "'";
//		}
//		sql += " ORDER BY ship_date,ship_time";
//		List<Ret_ship_notice> retShipNoticeList = retShipNoticeDao.find(sql);
//
//		if (retShipNoticeList != null) {
//			for (int i=0; i< retShipNoticeList.size();i++) {
//				XpaplydnOA oary = new XpaplydnOA();
//				if(i>0){
//					if(retShipNoticeList.get(i).getDn_no().equals(retShipNoticeList.get(i-1).getDn_no())){
//					        continue;
//					}
//				}
//				oary.setEvt_seq_id(retShipNoticeList.get(i).getEvt_seq_id());
//				oary.setDn_no(retShipNoticeList.get(i).getDn_no());
//				oary.setDn_stat(getStatDsc(retShipNoticeList.get(i).getDn_stat()));//DN状态装换中文
//				oary.setShip_address(retShipNoticeList.get(i).getShip_address());
//				oary.setShip_car_id(retShipNoticeList.get(i).getShip_car_id());
//				if (retShipNoticeList.get(i).getShip_date() != null) {
//					oary.setShip_date(retShipNoticeList.get(i).getShip_date());
//				}
//				oary.setShip_note(retShipNoticeList.get(i).getShip_note());
//				oary.setShip_qty(retShipNoticeList.get(i).getShip_qty());
//				oary.setShip_time(retShipNoticeList.get(i).getShip_time());
//				oary.setShip_usr(retShipNoticeList.get(i).getShip_usr());
//				oary.setEvt_usr(retShipNoticeList.get(i).getEvt_usr());
//				oary.setEvt_timestamp(retShipNoticeList.get(i).getEvt_timestamp());
//				oary.setDn_wh_out_prd_qty(retShipNoticeList.get(i).getWh_out_prd_qty());
//				oary.setSo_id_fk(retShipNoticeList.get(i).getSo_id_fk());
//				oary.setWo_id_fk(retShipNoticeList.get(i).getWo_id_fk());
//
//				oary.setCus_id(retShipNoticeList.get(i).getCus_id_fk());
//				oary.setShip_cus_id(retShipNoticeList.get(i).getShip_cus_id_fk());
//				oary.setCrt_timestamp(retShipNoticeList.get(i).getCrt_timestamp());
//				oary.setCrt_usr(retShipNoticeList.get(i).getCrt_usr());
//				oary.setShipping_timestamp(retShipNoticeList.get(i).getShipping_timestamp());
//				if(retShipNoticeList.get(i).getShip_date() != null&&retShipNoticeList.get(i).getShip_time()!=null){
//				   oary.setPlan_shipping_timestamp(retShipNoticeList.get(i).getShip_date().toString()+"_"+retShipNoticeList.get(i).getShip_time().toString());
//				}
//				oary.setDn_unique(retShipNoticeList.get(i).getDn_no()+"/"+oary.getPlan_shipping_timestamp()+"@"+retShipNoticeList.get(i).getEvt_seq_id());
//				oary.setDn_uniquetime(retShipNoticeList.get(i).getDn_no()+"/"+oary.getPlan_shipping_timestamp());
//				DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//				Timestamp ts = new Timestamp(format.parse(retShipNoticeList.get(i).getShip_date().toString()+" "+retShipNoticeList.get(i).getShip_time().toString()).getTime());
//				oary.setPlan_timestamp(ts);
//				Ret_wo retWo = retWODao.get(retShipNoticeList.get(i).getWo_id_fk());
//				if(retWo!=null){
//					oary.setMtrl_prod_id_fk(retWo.getMtrl_prod_id_fk());
//					oary.setMdl_id_fk(retWo.getMdl_id_fk());
//					oary.setFm_mdl_id_fk(retWo.getFm_mdl_id_fk());
//					oary.setCut_mdl_id_fk(retWo.getCut_mdl_id_fk());
//					oary.setPln_prd_qty(retWo.getPln_prd_qty());
//					oary.setWh_in_prd_qty(retWo.getWh_in_prd_qty()+retWo.getWip_wh_in_prd_qty());
//					oary.setWh_out_prd_qty(retWo.getWh_out_prd_qty());
//					oary.setScrp_prd_qty(retWo.getScrp_prd_qty());
//					oary.setFrom_thickness(retWo.getFrom_thickness());
//					oary.setTo_thickness(retWo.getTo_thickness());
//					oary.setRl_prd_qty(retWo.getRl_prd_qty());
//					oary.setWo_rsv_dn_qty(retWo.getRsv_dn_qty());
//					oary.setWo_space_dn_qty(retWo.getRl_prd_qty()-retWo.getRsv_dn_qty());
//					
//					String so_id_str = retWo.getSo_id();
//					if (!Util.isSpaceCheck(so_id_str) && so_id_str.length() > 2) {
//						if (so_id_str.substring(0, 2).equals("DQR")) {
//						}else {
//							oary.setReal_so_id(so_id_str);
//						}
//					}else {
//						oary.setReal_so_id("");
//					}
//				}else {
//					String so_id_str = retShipNoticeList.get(i).getSo_id_fk();
//					if (!Util.isSpaceCheck(so_id_str) && so_id_str.length() > 2) {
//						if (so_id_str.substring(0, 3).equals("DQR")) {
//						}else {
//							oary.setReal_so_id(so_id_str);
//						}
//					}else {
//						oary.setReal_so_id("");
//					}
//				}
//				oaryList.add(oary);
//			}
//		}
//		outTrx.setRtn_code(RETURN_CODE_OK);
//		outTrx.setRtn_mesg(RETURN_MESG_OK);
//		outTrx.setOary(oaryList);
//		outTrx.setTbl_cnt(oaryList.size());
//	}
	//new
	public void getDnProcquchong() {
		XpaplydnIA iary = inTrx.getIary().get(0);
		String sql = "select distinct dn_no FROM Ret_ship_notice where cus_id_fk='" + iary.getCus_id() + "'";
		List<String> retShipNoticeList = retShipNoticeDao.findDistStringList(sql);
		List<XpaplydnOA> oaryList = new ArrayList<XpaplydnOA>();
		if (retShipNoticeList != null && retShipNoticeList.size() > 0) {
			for (String dn_no : retShipNoticeList) {
				XpaplydnOA oary = new XpaplydnOA();
				oary.setDn_no(dn_no);
				oaryList.add(oary);
			}
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		outTrx.setOary(oaryList);
		outTrx.setTbl_cnt(oaryList.size());
	}
}
